package cn.e3.sso.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import cn.e3.jedis.JedisClient;
import cn.e3.pojo.TbUser;
import cn.e3.sso.service.TokenService;
import cn.e3.utils.E3Result;
import cn.e3.utils.JsonUtils;
@Service
public class TokenServiceImpl implements TokenService{
	@Autowired
	private JedisClient jedisClient;
	@Value("${SESSION_EXPIRE}")
	private int SESSION_EXPIRE;
	@Override
	public E3Result getUserByToken(String token) {
		//根据key值取出user信息
		String json = jedisClient.get("SessionId"+token);
		//判断用户信息是否存在
		if(json==null){
			return	E3Result.build(302, "登录信息过期或未登录，清从新登陆");
			
		}
		//用户存在，将json装换为对象
		TbUser user = JsonUtils.jsonToPojo(json, TbUser.class);
		//更新过期时间
		jedisClient.expire("SessionId"+token, SESSION_EXPIRE);
		//将user封装到e3
		return E3Result.ok(user);
	}

}
